package com.cloud.bc.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.cloud.bc.model.entity.BcCategoryDistricts;
import com.cloud.bc.mapper.BcCategoryDistrictsMapper;
import com.cloud.bc.model.enums.RedisKey;
import com.cloud.bc.model.request.DistrictConvertRequest;
import com.cloud.bc.model.request.VoiceListRequest;
import com.cloud.bc.service.BcCategoryDistrictsService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.model.enums.RedisKeyEnum;
import com.cloud.redis.config.RedisUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 地区分类
 *
 * @author douhaichao code generator
 * @date 2021-01-03 11:24:32
 */
@Service
@AllArgsConstructor
public class BcCategoryDistrictsServiceImpl extends ServiceImpl<BcCategoryDistrictsMapper, BcCategoryDistricts> implements BcCategoryDistrictsService {
    private final RedisUtils redisUtils;

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcCategoryDistricts selectOne(BcCategoryDistricts req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcCategoryDistricts>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcCategoryDistricts> selectAll(BcCategoryDistricts req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcCategoryDistricts> selectPage(Page page, BcCategoryDistricts req) {
        IPage<BcCategoryDistricts> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcCategoryDistricts>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcCategoryDistricts> selectPageByLike(Page page, BcCategoryDistricts req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (null != req.getParentId()) {
            queryWrapper.eq("parent_id", req.getParentId());
        }

        if (!StrUtil.isEmpty(req.getName())) {
            queryWrapper.like("name", req.getName());
        }

        if (null != req.getLevel()) {
            queryWrapper.eq("level", req.getLevel());
        }

        if (null != req.getSort()) {
            queryWrapper.eq("sort", req.getSort());
        }

        if (!StrUtil.isEmpty(req.getNote())) {
            queryWrapper.like("note", req.getNote());
        }
        IPage<BcCategoryDistricts> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcCategoryDistricts req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    //查询所有
    @Override
    public List<BcCategoryDistricts> selectAllById(String[] list) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", list);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BcCategoryDistricts> selectAllById(Set<Integer> list) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", list);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void getCityConvert(DistrictConvertRequest reqParam) {
        Set<Integer> list = new HashSet<>();
        String district = reqParam.getDistrict();
        String nativePlace = reqParam.getNativePlace();
        if (!StrUtil.isEmpty(district)) {
            String[] districtArr = district.split("\\.");
            for (String item :
                    districtArr) {
                if (StrUtil.isEmpty(item)) {
                    continue;
                }
                list.add(Convert.toInt(item));
            }
        }
        if (!StrUtil.isEmpty(nativePlace)) {
            String[] nativePlaceArr = nativePlace.split("\\.");
            for (String item :
                    nativePlaceArr) {
                if (StrUtil.isEmpty(item)) {
                    continue;
                }
                list.add(Convert.toInt(item));
            }
        }
        if (list.size() > 0) {
            List<BcCategoryDistricts> bcCategoryDistricts = selectAllById(list);
            if (!StrUtil.isEmpty(district)) {
                String[] districtArr = district.split("\\.");
                String districtCn = "";

                for (String item : districtArr) {
                    BcCategoryDistricts bcCategoryDistrictsResult =
                            bcCategoryDistricts.stream().filter(t -> t.getId().equals(Convert.toInt(item))).findFirst().orElse(new BcCategoryDistricts());
                    String name = bcCategoryDistrictsResult.getName();
                    if (!StrUtil.isEmpty(bcCategoryDistrictsResult.getName())) {
                        districtCn += name + ".";
                    } else {
                        districtCn += "不限" + ".";
                    }
                }

                reqParam.setDistrictCn(StrUtil.removeSuffix(districtCn, "."));
            }
            if (!StrUtil.isEmpty(nativePlace)) {
                String nativePlaceCn = "";
                String[] nativePlaceArr = nativePlace.split("\\.");
                for (String item : nativePlaceArr) {
                    BcCategoryDistricts bcCategoryDistrictsResult =
                            bcCategoryDistricts.stream().filter(t -> t.getId().equals(Convert.toInt(item))).findFirst().orElse(new BcCategoryDistricts());
                    String name = bcCategoryDistrictsResult.getName();
                    if (!StrUtil.isEmpty(bcCategoryDistrictsResult.getName())) {
                        nativePlaceCn += name + ".";
                    } else {
                        nativePlaceCn += "不限" + ".";
                    }
                }
                reqParam.setNativePlaceCn(StrUtil.removeSuffix(nativePlaceCn, "."));
            }
        }
    }

    @Override
    public List<BcCategoryDistricts> getAllCategoryDistrictsCache() {
        String key = RedisKeyEnum.bcCategoryDistrictsCache.getTitle();
        Object obj = redisUtils.get(key);
        String data = Convert.toStr(obj);
        if (StrUtil.isNotEmpty(data)) {
            return JSONUtil.toList(data, BcCategoryDistricts.class);
        }
        List<BcCategoryDistricts> bcCategoryDistricts = baseMapper.selectList(null);
        String s = JSONUtil.toJsonStr(bcCategoryDistricts);
        redisUtils.set(key, s);
        return bcCategoryDistricts;
    }
}
